package io.renren.modules.aic.controller;

import io.renren.common.annotation.SysLog;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.aic.entity.NoticeEntity;
import io.renren.modules.aic.service.NoticeService;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 系统公告
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2018-04-01 14:58:42
 */
@RestController
@RequestMapping("aic/notice")
public class NoticeController {
	@Autowired
	private NoticeService noticeService;

	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("aic:notice:list")
	public R list(@RequestParam Map<String, Object> params) {
		PageUtils page = noticeService.queryPage(params);

		return R.ok().put("page", page);
	}

	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("aic:notice:info")
	public R info(@PathVariable("id") Long id) {
		NoticeEntity notice = noticeService.selectById(id);

		return R.ok().put("notice", notice);
	}

	/**
	 * 保存
	 */
	@SysLog("发布公告")
	@RequestMapping("/save")
	@RequiresPermissions("aic:notice:save")
	public R save(@RequestBody NoticeEntity notice) {
		// 校验类型
		ValidatorUtils.validateEntity(notice);
		notice.setCreateTime(new Date());
		noticeService.insert(notice);

		return R.ok();
	}

	/**
	 * 修改
	 */
	@SysLog("修改公告")
	@RequestMapping("/update")
	@RequiresPermissions("aic:notice:update")
	public R update(@RequestBody NoticeEntity notice) {
		// 校验类型
		ValidatorUtils.validateEntity(notice);
		noticeService.updateById(notice);

		return R.ok();
	}

	/**
	 * 删除
	 */
	@SysLog("删除公告")
	@RequestMapping("/delete")
	@RequiresPermissions("aic:notice:delete")
	public R delete(@RequestBody Long[] ids) {
		noticeService.deleteBatchIds(Arrays.asList(ids));

		return R.ok();
	}

}
